import json

from django.http.response import JsonResponse

from deploy.impl.bash_completion import job_deploy_bash_completion
from deploy.impl.docker import job_deploy_docker
from deploy.impl.etcd import job_deploy_etcd
from deploy.impl.kubernetes import job_deploy_kubernetes, job_add_node_to_kubernetes
from deploy.impl.nginx import job_deploy_nginx, job_deploy_k8s_lb_with_nginx


# todo 证书返回的结构的key需要标准定义一下
# todo 每个子任务添加回滚逻辑


def deploy_docker_view(request):
    params = json.loads(request.body.decode("utf-8"))
    code, message, data = job_deploy_docker(params)
    if code == 0:
        return JsonResponse({"code": 0, "message": "success", "data": data})
    return JsonResponse({"code": code, "message": message, "data": {}})


def deploy_etcd_view(request):
    params = json.loads(request.body.decode("utf-8"))
    code, message, data = job_deploy_etcd(params)
    if code == 0:
        return JsonResponse({"code": 0, "message": "success", "data": data})
    return JsonResponse({"code": code, "message": message, "data": {}})


def deploy_nginx_view(request):
    params = json.loads(request.body.decode("utf-8"))
    code, message, data = job_deploy_nginx(params)
    if code == 0:
        return JsonResponse({"code": 0, "message": "success", "data": data})
    return JsonResponse({"code": code, "message": message, "data": {}})


def deploy_k8s_lb_with_nginx_view(request):
    params = json.loads(request.body.decode("utf-8"))
    code, message, data = job_deploy_k8s_lb_with_nginx(params)
    if code == 0:
        return JsonResponse({"code": 0, "message": "success", "data": data})
    return JsonResponse({"code": code, "message": message, "data": {}})


def deploy_bash_completion_view(request):
    params = json.loads(request.body.decode("utf-8"))
    code, message, data = job_deploy_bash_completion(params)
    if code == 0:
        return JsonResponse({"code": 0, "message": "success", "data": data})
    return JsonResponse({"code": code, "message": message, "data": {}})


def deploy_kubernetes_view(request):
    params = json.loads(request.body.decode("utf-8"))
    code, message, data = job_deploy_kubernetes(params)
    if code == 0:
        return JsonResponse({"code": 0, "message": "success", "data": data})
    return JsonResponse({"code": code, "message": message, "data": {}})


def add_node_to_kubernetes_cluster_view(request):
    params = json.loads(request.body.decode("utf-8"))
    code, message, data = job_add_node_to_kubernetes(params)
    if code == 0:
        return JsonResponse({"code": 0, "message": "success", "data": data})
    return JsonResponse({"code": code, "message": message, "data": {}})
